# -* - coding: UTF-8 -* -
#import sys
#reload(sys)
#sys.setdefaultencoding('utf8')

import threading,string,os
from manage.commander import *
from manage.thread import *
from time import sleep
from task import *
import logging

#从外部队列读取消息放入内部对列
class ManageCenter():  
	def __init__(self,coop,serverFactory,service_center,config):
		self.coop = coop
		self.serverFactory=serverFactory
		self.service_center=service_center
		self.config=config
		self.threads=[]
		self.create()
	
		
	#生成队列
	def create(self):
		#RPC队列
		self.manage_server=self.serverFactory.create(self.config,self.config.get('queue_server','manage_name','manage'))
		manage_count=self.config.get_int('coop','manage_count',1)
		for i in range(manage_count):#10线程执行
			commander=ManageCommander(self.coop)
			t = ManageThread(self.manage_server,commander,self.service_center)  
			self.threads.append(t)	
			
	#启动
	def start(self):
		for t in self.threads:
			t.start_thread()
	
	#停止
	def stop(self):
		logging.info("MANAGE Thread Stop!")			
		for t in self.threads:
			t.stop_thread()	
	#停止
	def terminate(self):
		logging.info("MANAGE Thread Stop!")			
		for t in self.threads:
			t.terminate()				

